/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import DAL.HotelBean;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Mr-aUgust
 */
@WebServlet(name = "ControllerHotels", urlPatterns = {"/ControllerHotels"})
public class ControllerHotels extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HotelBean htbean=new HotelBean();
        HttpSession session=request.getSession();
        if(request.getParameter("createhotel")!=null)
        {
             if(request.getParameter("createhotel").equals("Create Hotel"))
             {
                 String image=request.getParameter("image");
                 int locaID=Integer.parseInt(request.getParameter("location"));
                 String hotelName=request.getParameter("hotelname");
                 String address=request.getParameter("address");
                 String standant=request.getParameter("standard");
                 float price=Float.parseFloat(request.getParameter("price"));
                 if(htbean.InsertHotels(image,locaID,hotelName,address,standant,price)==true)
                 {
                      out.println("<script>alert('Create hotel Success!');window.location='Admin/management.jsp?page=Hotels';</script>");
                 }
                 else
                 {
                      out.println("<script>alert('Create hotel fail!');javascript:window.history.back(-1);</script>");
                 }
             }
        }
        if(request.getParameter("edit")!=null)
        {
             if(request.getParameter("edit").equals("Edit"))
             {
                String hotelID=request.getParameter("hotelID");
                String image=request.getParameter("image");
                String nameCity=request.getParameter("nameCity");
                String hotelName=request.getParameter("hotelName");
                String address=request.getParameter("address");
                String standard=request.getParameter("standard");
                String price=request.getParameter("price");
                
                session.setAttribute("hotelID",hotelID);
                session.setAttribute("image",image);
                session.setAttribute("nameCity",nameCity);
                session.setAttribute("hotelName",hotelName);
                session.setAttribute("address",address);
                session.setAttribute("standard",standard);
                session.setAttribute("price",price);
                session.setAttribute("statusH","True");
                
                response.sendRedirect("Admin/management.jsp?page=Hotels");
             }
        }
        if(request.getParameter("updatehotel")!=null)
        {
             if(request.getParameter("updatehotel").equals("Update Hotel"))
             {
                int hotelID=Integer.parseInt(request.getParameter("hotelId"));
                int nameCity=Integer.parseInt(request.getParameter("location"));
                String hotelName=request.getParameter("hotelname");
                String address=request.getParameter("address");
                String standard=request.getParameter("standard");
                float price=Float.parseFloat(request.getParameter("price"));
                String image="";                
                String imageDb="";
                if(request.getParameter("image")!=null)
                {
                      image=request.getParameter("image");
                      
                      if(htbean.UpdateYesImageHotel(image,nameCity,hotelName,address,standard,price,hotelID)==true)
                      {
                            session.removeAttribute("hotelID");
                             session.removeAttribute("image");
                             session.removeAttribute("nameCity");
                             session.removeAttribute("hotelName");
                             session.removeAttribute("address");
                             session.removeAttribute("standard");
                             session.removeAttribute("price");
                             session.removeAttribute("statusH");
                          out.println("<script>alert('Update hotel Success!');window.location='Admin/management.jsp?page=Hotels';</script>");
                      }
                      else
                      {
                          out.println("<script>alert('Update hotel fail!');javascript:window.history.back(-1);</script>");
                      }
                }
                else
                {
                    if(htbean.UpdateHotel(nameCity,hotelName,address,standard,price,hotelID)==true)
                        {
                             session.removeAttribute("hotelID");
                             session.removeAttribute("image");
                             session.removeAttribute("nameCity");
                             session.removeAttribute("hotelName");
                             session.removeAttribute("address");
                             session.removeAttribute("standard");
                             session.removeAttribute("price");
                             session.removeAttribute("statusH");
                            out.println("<script>alert('Update hotel Success!');window.location='Admin/management.jsp?page=Hotels';</script>");
                        }
                        else
                        {
                             out.println("<script>alert('Update hotel fail!');javascript:window.history.back(-1);</script>");
                        }    
                }
             }
        }
         if(request.getParameter("delete")!=null)
        {
             if(request.getParameter("delete").equals("Delete"))
             {
                 int hotelID=Integer.parseInt(request.getParameter("hotelID"));
                 if(htbean.DeleteHotel(hotelID)==true)
                 {
                     out.println("<script>alert('Delete hotel Success!');window.location='Admin/management.jsp?page=Hotels';</script>");
                 }
                 else
                 {
                     out.println("<script>alert('Delete hotel fail!');javascript:window.history.back(-1);</script>");
                 }
             }
         }
         if(request.getParameter("reset")!=null)
        {
             if(request.getParameter("reset").equals("Cancel Update"))
             {
                     session.removeAttribute("hotelID");
                     session.removeAttribute("image");
                     session.removeAttribute("nameCity");
                     session.removeAttribute("hotelName");
                     session.removeAttribute("address");
                     session.removeAttribute("standard");
                     session.removeAttribute("price");
                     session.removeAttribute("statusH");
                     response.sendRedirect("Admin/management.jsp?page=Hotels");
             }
         }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
